import { http } from '@/utils/request';
import type { ApiResponse } from '@/types';

// 题解相关类型定义
export interface Solution {
  id: number;
  user_id: number;
  question_id: number;
  content: string;
  summary?: string;
  created_at: string;
  updated_at: string;
  user: {
    id: number;
    username: string;
    avatar: string;
  };
}

export interface SolutionDetail extends Solution {
  // 详情页面可能包含更多信息
}

export interface SolutionListResponse {
  solutions: Solution[];
  pagination: {
    page: number;
    page_size: number;
    total: number;
    total_page: number;
  };
}

export interface SolutionQuery {
  page?: number;
  page_size?: number;
  user_id?: string;
}

export interface CreateSolutionRequest {
  content: string;
}

export interface UpdateSolutionRequest {
  content: string;
}

export const solutionApi = {
  // 获取题目的题解列表
  getSolutions(questionId: number, params?: SolutionQuery): Promise<ApiResponse<SolutionListResponse>> {
    return http.get(`/v1/solutions/question/${questionId}`, { params });
  },

  // 获取题解详情
  getSolutionDetail(solutionId: number): Promise<ApiResponse<SolutionDetail>> {
    return http.get(`/v1/solutions/${solutionId}`);
  },

  // 创建题解
  createSolution(questionId: number, data: CreateSolutionRequest): Promise<ApiResponse<{ id: number }>> {
    return http.post(`/v1/solutions/question/${questionId}`, data);
  },

  // 更新题解
  updateSolution(solutionId: number, data: UpdateSolutionRequest): Promise<ApiResponse<void>> {
    return http.put(`/v1/solutions/${solutionId}`, data);
  },

  // 删除题解
  deleteSolution(solutionId: number): Promise<ApiResponse<void>> {
    return http.delete(`/v1/solutions/${solutionId}`);
  },
};
